/*
 * @Author: 来自火星的码农 15122322+heyzhi@user.noreply.gitee.com
 * @Date: 2025-10-10 10:33:01
 * @LastEditors: 来自火星的码农 15122322+heyzhi@user.noreply.gitee.com
 * @LastEditTime: 2025-10-10 10:50:15
 * @FilePath: /GateServer/src/mysql/mysqlconnectionpool.h
 * @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
 */
#pragma once

#include "mysql_connection.h"
#include <condition_variable>
#include <cppconn/connection.h>
#include <cstddef>
#include <memory>
#include <mutex>
#include <queue>
#include "memory"
#include "atomic"
namespace Yizhi {
    class MysqlConnectionPool{
        public:

        MysqlConnectionPool(const std::string &url,const std::string& username,std::string& psw
        ,const std::string schema,size_t size=1);
        std::unique_ptr<sql::Connection> GetConnection();
        void close();        
        void ReturnConnection(std::unique_ptr<sql::Connection>);
        ~MysqlConnectionPool();

        private:
        ///@brief  数据库链接所需变量
        std::string m_url;
        std::string m_username;
        std::string m_pass;

        ///@brief 数据库名称
        std::string m_schema;
        

        ///@brief 链接数量
        size_t m_poolsize;

        ///@brief 互斥量
        std::mutex m_mutex;

        ///@brief 条件变量
        std::condition_variable m_cond;


        std::atomic<bool> m_stop={false};

        std::queue<std::unique_ptr<sql::Connection>> m_connections;

    };
}